﻿/* ************************************************************************************* 
 * CREATED BY:          LY HUNG SON
 * CREATED DATE:        08/07/2008
 * PROJECT:             MCCORP PROJECT
 * PURPOSE:             
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MCWEBSITE.BUL;
using MCWEBSITE.ENTITY;

public partial class Modules_WebsiteBackEnd_wucNewsCategoriesDetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region "BINDING DATA CONTROLS"

    public void BindDataControl()
    {
        this.BindLanguageData();
        this.BindParentCategoryData(rcboLanguageCode.SelectedValue);
    }

    protected void BindLanguageData()
    {
        // Load rcboLanguageCode
        rcboLanguageCode.Items.Clear();
        List<websiteLanguageInfo> LanguageList = websiteLanguageBUL.GetAll();
        rcboLanguageCode.DataTextField = "languageName";
        rcboLanguageCode.DataValueField = "LanguageCode";
        rcboLanguageCode.DataSource = LanguageList;
        rcboLanguageCode.DataBind();
    }

    protected void BindParentCategoryData(string LanguageCode)
    {
        // Load rcboParentCategory
        rcboParentCategory.Items.Clear();
        List<websiteNewsCategoryInfo> ParentCategoryList = websiteNewsCategoryBUL.GetParentByLanguageCode(LanguageCode);
        websiteNewsCategoryInfo SelectAllNewsCategory = new websiteNewsCategoryInfo();
        SelectAllNewsCategory.CategoryID = 0;
        SelectAllNewsCategory.CategoryName = "DANH MỤC GỐC";
        SelectAllNewsCategory.CategoryCode = "";
        SelectAllNewsCategory.Description = "";
        SelectAllNewsCategory.Atribute1 = "";
        SelectAllNewsCategory.Atribute2 = "";
        SelectAllNewsCategory.Atribute3 = "";
        ParentCategoryList.Add(SelectAllNewsCategory);
        rcboParentCategory.DataSource = ParentCategoryList;
        rcboParentCategory.DataTextField = "CategoryName";
        rcboParentCategory.DataValueField = "CategoryID";
        rcboParentCategory.ClearSelection();
        rcboParentCategory.DataBind();
        rcboParentCategory.SelectedIndex = rcboParentCategory.FindItemIndexByValue("0");
    }

    protected void BindDataDetail(string ItemID, string LanguageCode, string ParentCategoryID)
    {
        if (ItemID != "" && ItemID != "-1" && ItemID != null && ItemID != "0")
        {
            websiteNewsCategoryInfo objNewsCategory = websiteNewsCategoryBUL.Load(Convert.ToInt32(ItemID));
            lblCategoryID.Text = objNewsCategory.CategoryID.ToString();
            tbCategoryCode.Text = objNewsCategory.CategoryCode.ToString();
            tbCategoryName.Text = objNewsCategory.CategoryName.ToString();
            tbDescription.Text = objNewsCategory.Description.ToString();
            rcboLanguageCode.Items.FindItemByValue(Convert.ToString(objNewsCategory.LanguageCode)).Selected = true;
            this.BindParentCategoryData(objNewsCategory.LanguageCode);
            rcboParentCategory.Items.FindItemByValue(objNewsCategory.ParentCategoryID != -1 ? Convert.ToString(objNewsCategory.ParentCategoryID) : "0").Selected = true;
        }
        else
        {
            lblCategoryID.Text = "";
            tbCategoryCode.Text = "";
            tbCategoryName.Text = "";
            tbDescription.Text = "";
            if (LanguageCode != "TOÀN BỘ")
                rcboLanguageCode.Items.FindItemByValue(LanguageCode).Selected = true;
            this.BindParentCategoryData(rcboLanguageCode.SelectedValue);
            rcboParentCategory.Items.FindItemByValue(ParentCategoryID).Selected = true;
        }
    }

    #endregion

    #region "DATA HANDLING PROCEDURE/FUNCTION"

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected int InsertRow()
    {
        websiteNewsCategoryInfo objNewsCategory = new websiteNewsCategoryInfo();
        objNewsCategory.CategoryID = 0;
        objNewsCategory.CategoryCode = tbCategoryCode.Text.Trim();
        objNewsCategory.CategoryName = tbCategoryName.Text.Trim();
        objNewsCategory.Description = tbDescription.Text.Trim();
        objNewsCategory.LanguageCode = Convert.ToString(rcboLanguageCode.SelectedValue);
        objNewsCategory.ParentCategoryID = Convert.ToInt32(rcboParentCategory.SelectedValue);
        objNewsCategory.Atribute1 = "";
        objNewsCategory.Atribute2 = "";
        objNewsCategory.Atribute3 = "";
        return websiteNewsCategoryBUL.Save(objNewsCategory);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected int DeleteRow()
    {
        return websiteNewsCategoryBUL.Remove(Convert.ToInt32(lblCategoryID.Text.Trim()));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected int UpdateRow()
    {
        websiteNewsCategoryInfo objNewsCategory = new websiteNewsCategoryInfo();
        objNewsCategory.CategoryID = Convert.ToInt32(lblCategoryID.Text.Trim()); ;
        objNewsCategory.CategoryCode = tbCategoryCode.Text.Trim();
        objNewsCategory.CategoryName = tbCategoryName.Text.Trim();
        objNewsCategory.Description = tbDescription.Text.Trim();
        objNewsCategory.LanguageCode = Convert.ToString(rcboLanguageCode.SelectedValue);
        objNewsCategory.ParentCategoryID = Convert.ToInt32(rcboParentCategory.SelectedValue);
        objNewsCategory.Atribute1 = "";
        objNewsCategory.Atribute2 = "";
        objNewsCategory.Atribute3 = "";
        return websiteNewsCategoryBUL.Save(objNewsCategory);
    }

    #endregion

    #region "CONTROL EVENT HANDLER"

    protected void rcboLanguageCode_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        this.BindParentCategoryData(rcboLanguageCode.SelectedValue);
    }

    #endregion

    #region "PUBLIC PROCEDURE/FUNCTION"
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ClickedButton"></param>
    /// <returns></returns>   
    public string PerformToolbarEvent(ToolbarButton ClickedButton)
    {
        string ResultString = "";
        int ReturnResult;

        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.NEW.ToString().ToUpper() &&
            ClickedButton == ToolbarButton.SAVEBUTTON)
        {
            if (Page.IsValid)
            {
                ReturnResult = this.InsertRow();
                if ((int)ReturnResult > 0) ResultString = "Bản ghi được bổ xung thành công";
                else ResultString = "Lỗi trong quá trình bổ xung bản ghi";
            }
        }

        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.EDIT.ToString().ToUpper() &&
            ClickedButton == ToolbarButton.SAVEBUTTON)
        {
            if (Page.IsValid)
            {
                ReturnResult = this.UpdateRow();
                if ((int)ReturnResult > 0) ResultString = "Bản ghi được cập nhật thành công";
                else ResultString = "Lỗi trong quá trình cập nhật bản ghi";
            }
        }

        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.EDIT.ToString().ToUpper() &&
            ClickedButton == ToolbarButton.DELBUTTON)
        {
            ReturnResult = this.DeleteRow();
            if ((int)ReturnResult > 0) ResultString = "Bản ghi được xóa thành công";
            else ResultString = "Lỗi trong quá trình xóa bản ghi";
        }

        return ResultString;
    }


    public void SetControlMode(ToolbarStatus ToolbarMode)
    {
        if (ToolbarMode == ToolbarStatus.NEW)
        {
            lblDetailMode.Text = "TẠO MỚI BẢN GHI";
            lblCurrentMode.Text = ToolbarStatus.NEW.ToString().ToUpper();
        }
        if (ToolbarMode == ToolbarStatus.EDIT)
        {
            lblDetailMode.Text = "CHỈNH SỬA BẢN GHI";
            lblCurrentMode.Text = ToolbarStatus.EDIT.ToString().ToUpper();
        }
    }

    public void SetDetailData(string ItemID, string LanguageCode, string ParentCategoryID)
    {
        this.BindDataControl();
        this.BindDataDetail(ItemID, LanguageCode, ParentCategoryID);
    }

    public void RefreshDetailView()
    {
        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.NEW.ToString().ToUpper())
        {
            string LanguageCode = rcboLanguageCode.SelectedValue;
            string ParentCategoryID = rcboParentCategory.SelectedValue;
            this.BindDataDetail("-1", LanguageCode, ParentCategoryID);
        }
        else
        {
            string SelectedItemID = lblCategoryID.Text.Trim();
            this.BindDataDetail(SelectedItemID, "-1", "-1");
        }
    }

    #endregion    

}
